package com.youzan.cloud.component.biz.member.service.impl;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.youzan.cloud.component.base.util.core.ObjectUtil;
import com.youzan.cloud.component.biz.member.common.util.LogRecordUtil;
import com.youzan.cloud.component.biz.member.common.util.OutResultUtil;
import com.youzan.cloud.component.biz.member.config.MemberLinkerConfiguration;
import com.youzan.cloud.component.biz.member.dal.dataobject.MemberInformation;
import com.youzan.cloud.component.biz.member.dal.mapper.MemberInformationMapper;
import com.youzan.cloud.component.biz.member.dto.BaseResult;
import com.youzan.cloud.component.biz.member.dto.LogDTO;
import com.youzan.cloud.component.biz.member.dto.MemberBaseDto;
import com.youzan.cloud.component.biz.member.dto.MemberUpdateInputDto;
import com.youzan.cloud.component.biz.member.dto.linker.out.MemberLinkerBaseResult;
import com.youzan.cloud.component.biz.member.youzanopen.api.YzMemberApiService;
import com.youzan.cloud.connect.service.common.domain.BizServiceInfo;
import com.youzan.cloud.connect.service.sdk.ConnectServiceResponse;
import com.youzan.cloud.connect.service.sdk.exception.ConnectServiceCallException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author tangsen233
 * @version 1.0
 * @description: ISV推送会员信息
 * @date 2021/8/9 20:35
 */
@Service
@Slf4j
public class MemberUpdateService extends AbstractService {

    @Autowired
    YzMemberApiService yzMemberApiService;

    @Autowired
    private MemberLinkerConfiguration memberLinkerConfiguration;

    @Autowired
    private LogRecordUtil logRecordUtil;

    @Autowired
    private MemberInformationMapper memberInformationMapper;

    @Override
    public LogDTO buildLog(Object inputParam) {
        return null;
    }

    @Override
    public BaseResult handleException(Exception e) {
       return OutResultUtil.fail("组件异常!","500",e);
    }

    @Override
    public BaseResult doBiz(MemberBaseDto memberBaseDto) {
        if(ObjectUtil.isEmpty(memberBaseDto) || ObjectUtil.isEmpty(memberBaseDto.getMemberUpdateDto()) || ObjectUtil.isEmpty(memberBaseDto.getYzMemberUpdateDto())){
          return   OutResultUtil.fail("参数为空","500",null);
        }
        MemberUpdateInputDto memberUpdateInputDto = JSONObject.parseObject(JSONObject.toJSONString(memberBaseDto.getYzMemberUpdateDto()), MemberUpdateInputDto.class);
        BizServiceInfo memberUpdateLinker = memberLinkerConfiguration.getMemberUpdateLinker();
        if(ObjectUtil.isEmpty(memberUpdateLinker)){
            return OutResultUtil.fail("会员修改信息linker参数为空","500",null);
        }
        MemberInformation memberInformation = new MemberInformation();
        BeanUtils.copyProperties(memberUpdateInputDto,memberInformation);
        memberInformation.setYzOpenid(memberBaseDto.getYzOpenId());
        ConnectServiceResponse<MemberLinkerBaseResult> memberUpdateResult = null;
        Long logRecordId = null;
        try {
            memberUpdateResult = connectServiceCaller.callConnectService(memberUpdateLinker.getConnectServiceName(),memberUpdateLinker.getTargetSystem(),memberUpdateLinker.getTargetSystemVersion(),memberBaseDto.getMemberUpdateDto());
            logRecordId  = logRecordUtil.saveLogRecord(memberUpdateLinker.getConnectServiceName(), memberUpdateLinker.getConnectServiceName(), JSON.toJSONString(memberBaseDto.getMemberUpdateDto()), memberUpdateResult.getResponseDTO().toString());
            memberInformation.setLogId(logRecordId);
            memberInformationMapper.updateAllByYzOpenId(memberInformation);
        }catch (ConnectServiceCallException e){
            log.error("调用会员修改连接器异常"+e.getMessage());
            return OutResultUtil.fail(memberUpdateResult.getErrMsg(),memberUpdateResult.getErrorCode().toString(),memberUpdateResult);
        }
        if(memberUpdateResult.isSuccess()){
            return OutResultUtil.success(memberUpdateResult);
        }
        return OutResultUtil.fail(memberUpdateResult.getErrMsg(),memberUpdateResult.getErrorCode().toString(),memberUpdateResult);
    }
}